home *** CD-ROM | disk | FTP | other *** search
-
- /***************************************\
- * *
- * switchf.c *
- * Gdos-less Gdos font switcher *
- * soon to appear in Gulam, Xmdm *
- * and Zmdm *
- * *
- * Jwahar R. Bammi *
- * {decvax,cbosgd,sun}!cwruecmp!bammi *
- * CSnet,Arpa: bammi@cwru.edu *
- * CIS: 71515,155 *
- * *
- \***************************************/
-
-
- #include <stdio.h>
- #include "aline.h"
-
- #define SCREENFONT 2 /* index of 8x16 monochrome system default font */
-
- /* Possible Errors */
- #define ENO_ERROR 0 /* No error */
- #define EOPEN_FAIL 1 /* Error opening font file */
- #define EHEAD_READ 2 /* Error reading font header */
- #define EFORM_READ 3 /* Error reading font form */
- #define EOFF_READ 4 /* Error reading offset table */
- #define EMEMORY 5 /* Outa memory */
-
- /* Globals */
- FONT *system_font; /* pointer to default system font */
- int ferr; /* error # if any */
-
-
- /*
- * Init aline & get system font pointer
- */
- init()
- {
- init_aline();
- system_font = fonts[SCREENFONT]; /* save it */
- }
-
- /*
- * Switch to given Gdos font (pointer)
- * Acknowledgements to
- * Martin Minow
- * minow%thundr.dec@decwrl.dec.com
- * decvax!minow
- * for discovering the info in the routine below. thanks!
- */
- switch_font(fp)
- FONT *fp;
- {
- /* See aline.h for description of fields */
-
- naline->V_CEL_HT = fp->form_height;
- naline->V_CEL_WR = aline->VWRAP * fp->form_height;
- naline->V_CEL_MY = (naline->V_Y_MAX / fp->form_height) - 1;
- naline->V_CEL_MX = (naline->V_X_MAX / fp->max_cell_width) - 1;
- naline->V_FNT_WR = fp->form_width;
- naline->V_FNT_ST = fp->first_ade;
- naline->V_FNT_ND = fp->last_ade;
- naline->V_OFF_AD = fp->off_table;
- naline->V_FNT_AD = fp->dat_table;
- }
-
- /*
- * Swap bytes of a word
- */
- static VOID Swap(p)
- unsigned char p[];
- {
- register unsigned char t;
-
- t = p[0];
- p[0] = p[1];
- p[1] = t;
-
- }
-
-
- /*
- * Swap all entries in Intel format to MC68K format WORDs
- *
- */
- static VOID fix_font(font)
- register FONT *font;
- {
-
- Swap(&font->font_id);
- Swap(&font->size);
- Swap(&font->first_ade);
- Swap(&font->last_ade);
- Swap(&font->top);
- Swap(&font->ascent);
- Swap(&font->half);
- Swap(&font->descent);
- Swap(&font->bottom);
- Swap(&font->max_char_width);
- Swap(&font->max_cell_width);
- Swap(&font->left_offset);
- Swap(&font->right_offset);
- Swap(&font->thicken);
- Swap(&font->ul_size);
- Swap(&font->lighten);
- Swap(&font->skew);
- Swap(&font->flags);
- Swap(&font->form_width);
- Swap(&font->form_height);
-
- /* init only */
- font->h_table = (char *)NULL;
- font->next_font = (FONT *)NULL;
- }
-
-
-
- /*
- * Load a font, return font pointer
- * On error: ferr contains error # and pointer returned is NULL
- */
- FONT *load_font(name)
- char *name;
- {
- register WORD i,j;
- register FONT *font;
- WORD *otable;
- WORD ndata, nchar;
- FILE *fp;
- extern char *malloc();
- #ifdef ALCYON
- extern FILE *fopenb();
- #else
- extern FILE *fopen();
- #endif
-
- ferr = ENO_ERROR;
-
- /* open it up */
- #ifdef ALCYON
- if((fp = fopenb(name, "r")) == (FILE *)NULL)
- #else
- if((fp = fopen(name, "rb")) == (FILE *)NULL)
- #endif
- {
- ferr = EOPEN_FAIL;
- return (FONT *)NULL;
- }
-
- /* alloc font header */
- if((font = (FONT *)malloc(sizeof(FONT))) == (FONT *)NULL)
- {
- ferr = EMEMORY;
- return (FONT *)NULL;
- }
-
- if(fread(font, sizeof(FONT), 1, fp) != 1)
- {
- ferr = EHEAD_READ;
- return (FONT *)NULL;
- }
-
- /* Fix WORD fields and init */
- fix_font(font);
-
- /* alloc offset table */
- nchar = font->last_ade - font->first_ade + 1; /* # of chars in font */
- /* all we are doing here is if first_ade is > 30, we are makeing
- * first_ade = 0, by adding 30 entries before first_ade and
- * moving first_ade to 0, otherwise Bconout([2,5],X) will not
- * work (they bomb) - Yet Another Undocumented 'Feature' :-)
- */
-
- /* add one for the last offset */
- i = (((nchar+30) < 256)? nchar+30 : nchar + (256 - nchar)) + 1;
- if((font->off_table = (WORD *)malloc(i*2)) == (WORD *)NULL)
- {
- ferr = EMEMORY;
- return (FONT *)NULL;
- }
- /* init it */
- for(j = 0; j < i; j++)
- font->off_table[j] = 0;
-
- otable = ((nchar+30) < 256)? &font->off_table[30] :
- &font->off_table[(256-nchar)];
- font->first_ade = 0;
-
- /* alloc font form table */
- ndata = font->form_width * font->form_height; /* bytes in font form */
- if((font->dat_table = malloc(ndata)) == (char *)NULL)
- {
- ferr = EMEMORY;
- return (FONT *)NULL;
- }
-
- /* read in offset table */
- if(fread(otable, 2, nchar, fp) != nchar)
- {
- ferr = EOFF_READ;
- return (FONT *)NULL;
- }
- /* swap WORDs */
- for(i = 0; i <= nchar; i++)
- Swap(&otable[i]);
-
- /* read in font form */
- if(fread(font->dat_table, 1, ndata, fp) != ndata)
- {
- ferr = EFORM_READ;
- return (FONT *)NULL;
- }
- fclose(fp);
-
- /* and finally return the pointer to font header */
- return font;
- }
-
- /*
- * free up a loaded font
- */
- VOID free_font(font)
- FONT *font;
- {
- free(font->dat_table);
- free(font->off_table);
- free(font);
- }
-
- #ifdef TEST
- /*
- * load and show fonts
- *
- */
- main(argc, argv)
- WORD argc;
- char **argv;
- {
- register WORD i, j;
- register FONT *font_ptr;
- extern FONT *load_font();
-
- if(argc < 2)
- {
- fprintf(stderr,"Usage: switchf fontfiles ....\n");
- exit(1);
- }
-
- init();
- while(--argc > 0)
- {
- if((font_ptr = load_font(*++argv)) == (FONT *)NULL)
- fprintf(stderr,"Trouble Loading %s\n", *argv);
- else
- {
- fprintf(stderr,"\n%s:\n", *argv);
- switch_font(font_ptr);
- for(j = 0, i = ' '; i < font_ptr->last_ade; i++)
- {
- putchar(i);
- if((++j%8) == 0)
- putchar('\n');
- else
- putchar('\t');
- }
- putchar('\n');
- free(font_ptr);
- switch_font(system_font);
- }
- }
- exit(0);
- }
-
- #endif
-